package com.bawei.park2211a.news


import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.mvvm.BaseActivity
import com.bawei.base.room.NewsRoomEntity
import com.bawei.base.room.RoomUtlis

import com.bawei.park2211a.databinding.ActivityNewsBinding
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "/app/news/NewsActivity")
class NewsActivity : BaseActivity<ActivityNewsBinding,NewsViewModel>() {
  lateinit var newsAdapter: NewsAdapter

    override fun initView() {
        newsAdapter= NewsAdapter()
        lifecycleScope.launch {
            viewModel.getNewsByType().collect()  {
                newsAdapter.submitData(it)
            }
        }
        binding.newsRec.adapter=newsAdapter
        binding.newsRec.layoutManager=LinearLayoutManager(this)

        binding.newsAdd.setOnClickListener {
            ARouter.getInstance().build("/app/news/AddNewsActivity").navigation()
        }
        newsAdapter.setOnItemClickListener(object :NewsAdapter.OnItemClickListener{
            override fun onItemClick(position: Int) {
                    ARouter.getInstance().build("/app/news/NewsDetailActivity").navigation()
            }
        })
        newsAdapter.setOnItemLongClickListener(object :NewsAdapter.OnItemLongClickListener{
            override fun onItemLongClick(position: Int) {
                RoomUtlis.getNewsDao().insert(NewsRoomEntity(position,"这是第 ${position}条"))
                ToastUtils.showLong("添加成功")
            }
        })

    }

    override fun initData() {

    }
}